package me.zhengjie.controller.ui;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.dto.sqlServer.TaskResult;
import me.zhengjie.service.TaskResultServiceG;
import me.zhengjie.service.dto.TaskResultDto;
import me.zhengjie.service.dto.TaskResultQueryCriteria;
import me.zhengjie.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @website https://eladmin.vip
 * @author arden
 * @date 2025-06-16
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "1管理")
@RequestMapping("/api/taskResult")
public class TaskResultController {

    private final TaskResultServiceG taskResultService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('taskResult:list')")
    public void exportTaskResult(HttpServletResponse response, TaskResultQueryCriteria criteria) throws IOException {
        taskResultService.download(taskResultService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询1")
    @ApiOperation("查询1")
    @PreAuthorize("@el.check('taskResult:list')")
    @AnonymousAccess
    public ResponseEntity<PageResult<TaskResult>> queryTaskResult(TaskResultQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(taskResultService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增1")
    @ApiOperation("新增1")
    @PreAuthorize("@el.check('taskResult:add')")
    public ResponseEntity<Object> createTaskResult(@Validated @RequestBody TaskResult resources){
        taskResultService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改1")
    @ApiOperation("修改1")
    @PreAuthorize("@el.check('taskResult:edit')")
    public ResponseEntity<Object> updateTaskResult(@Validated @RequestBody TaskResult resources){
        taskResultService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除1")
    @ApiOperation("删除1")
    @PreAuthorize("@el.check('taskResult:del')")
    public ResponseEntity<Object> deleteTaskResult(@RequestBody Integer[] ids) {
        taskResultService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}